// m
// 给定一个正整数n，生成一个包含1到n平方的所有元素，且按照顺时针螺旋排列的n * n的正方形矩阵matrix

// 思路，定义上下左右边界，然后逆时针的顺序从边界上依次给数组相应位置赋值，当访问完当前边界之后，要更新一下边界位置，缩小范围，方便下一轮进行访问


function generateMatrix(n) {
    let matrix = new Array(n).fill(1).map(item => new Array(n).fill('_'))
    let up = 0
    let down = n - 1
    let left = 0
    let right = n - 1 
    let index = 1
    while (true) {
        for (let i = left; i <= right; i++) {
            matrix[up][i] = index
            index += 1
        }
        up += 1
        if (up > down) {
            break
        }
        for (let i = up; i <= down; i++) {
            matrix[i][right] = index
            index += 1
        }
        right -= 1
        if (right < left) {
            break
        }
        for (let i = right; i >= left; i--) {
            matrix[down][i] = index
            index += 1
        }
        down -= 1
        if (down < up) {
            break
        }
        for (let i = down; i >= up; i--) {
            matrix[i][left] = index
            index += 1
        }
        left += 1
        if (left > right) {
            break
        }
    }
    return matrix
}
console.log(generateMatrix(3));

function generateMatrix2(n) {
    let startI = 0
    let startJ = 0
    let offset = 1
    let count = 1
    let result = new Array(n).fill(0).map(() => new Array(n).fill(0))
    let loop = Math.floor(n / 2) // 圈数
    while (loop > 0) {
        let j = startJ
        let i = startI
        // n - offset算出来这一圈有几个数
        for (; j < startJ + n - offset; j++) {
            result[i][j] = count++
        }
        for (; i < startI + n - offset; i++) {
            result[i][j] = count++
        }
        for (; j > startJ; j--) {
            result[i][j] = count++
        }
        for (; i > startJ; i--) {
            result[i][j] = count++
        }
        offset+=2
        startI++
        startJ++
        loop--
    }
    if (n % 2 === 1) {
        let mid = Math.floor(n / 2)
        result[mid][mid] = count
    }
    return result
   
}

console.log(generateMatrix2(3));

/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix3 = function (n) {
    let mid = Math.floor(n / 2)
    let loop = mid
    let count = 1
    let offset = 1
    let startRow = 0
    let startCol = 0
    let row = 0
    let col = 0
    let result = new Array(n).fill(0).map(_ => new Array(n).fill(0))
    while (loop--) {
        row = startRow
        col = startCol
        while (col < n - offset) {
            result[row][col] = count++;
            col++
        }
        while (row < n - offset) {
            result[row][col] = count++;
            row++
        }
        while (col > startCol) {
            result[row][col] = count++;
            col--
        }
        while (row > startRow) {
            result[row][col] = count++;
            row--
        }
        offset++
        startCol++
        startRow++
    }
    if (n % 2 !== 0) {
        result[mid][mid] = count
    }
    return result

};

console.log(generateMatrix3(3));